import { TweenPlugin } from "./TweenPlugin";
import { TweenLite } from "../TweenLite";
type int = number;
//class PhysicsPropsPlugin
    
    export  class PhysicsPropsPlugin extends TweenPlugin
    {
        constructor()
        {
            
            this.propName = "physicsProps";
            this.overwriteProps = [];
            return;
        }

        public /* override */ onInitTween(arg1: Object, arg2, arg3: TweenLite): Boolean
        {
            var loc2=null;
            var loc3=null;
            this._target = arg1;
            this._tween = arg3;
            this._step = 0;
            var loc1=this._tween.timeline;
            while (loc1.timeline) 
            {
                loc1 = loc1.timeline;
            }
            if (loc1 == TweenLite.rootFramesTimeline) 
            {
                this._stepsPerTimeUnit = 1;
            }
            this._props = [];
            var loc4=0;
            var loc5=0;
            var loc6=arg2;
            for (loc2 in loc6) 
            {
                if (!((loc3 = arg2[loc2]).velocity || loc3.acceleration)) 
                {
                    continue;
                }
                var loc7;
                this._props[loc7 = loc4++] = new PhysicsProp(loc2, Number(arg1[loc2]), loc3.velocity, loc3.acceleration, loc3.friction, this._stepsPerTimeUnit);
                this.overwriteProps[loc4] = loc2;
                if (loc3.friction) 
                {
                    this._hasFriction = true;
                }
            }
            return true;
        }

        public /* override */ killProps(arg1: Object): void
        {
            var loc1=this._props.length;
            while (loc1--) 
            {
                if (!(this._props[loc1].property in arg1)) 
                {
                    continue;
                }
                this._props.splice(loc1, 1);
            }
            super.killProps(arg1);
            return;
        }

        public /* override */ set changeFactor(arg1: number)
        {
            var loc4=null;
            var loc5=0;
            var loc6=NaN;
            var loc7=0;
            var loc8=NaN;
            var loc9=NaN;
            var loc1=this._props.length;
            var loc2=this._tween.cachedTime;
            var loc3=[];
            if (this._hasFriction) 
            {
                loc5 = int(loc2 * this._stepsPerTimeUnit) - this._step;
                loc6 = loc2 * this._stepsPerTimeUnit % 1;
                if (loc5 >= 0) 
                {
                    while (loc1--) 
                    {
                        loc4 = this._props[loc1];
                        loc7 = loc5;
                        while (loc7--) 
                        {
                            loc4.v = loc4.v + loc4.a;
                            loc4.v = loc4.v * loc4.friction;
                            loc4.value = loc4.value + loc4.v;
                        }
                        loc3[loc1] = loc4.value + loc4.v * loc6;
                    }
                }
                else 
                {
                    while (loc1--) 
                    {
                        loc4 = this._props[loc1];
                        loc7 = -loc5;
                        while (loc7--) 
                        {
                            loc4.value = loc4.value - loc4.v;
                            loc4.v = loc4.v / loc4.friction;
                            loc4.v = loc4.v - loc4.a;
                        }
                        loc3[loc1] = loc4.value + loc4.v * loc6;
                    }
                }
                this._step = this._step + loc5;
            }
            else 
            {
                loc8 = loc2 * loc2 * 0.5;
                while (loc1--) 
                {
                    loc4 = this._props[loc1];
                    loc3[loc1] = loc4.start + (loc4.velocity * loc2 + loc4.acceleration * loc8);
                }
            }
            loc1 = this._props.length;
            if (this.round) 
            {
                while (loc1--) 
                {
                    loc9 = loc3[loc1];
                    this._target[PhysicsProp(this._props[loc1]).property] = loc9 > 0 ? int(loc9 + 0.5) : int(loc9 - 0.5);
                }
            }
            else 
            {
                while (loc1--) 
                {
                    this._target[PhysicsProp(this._props[loc1]).property] = Number(loc3[loc1]);
                }
            }
            return;
        }

        public static /* const */API: number=1;

        protected /* var */_tween: TweenLite;

        protected /* var */_target: Object;

        protected /* var */_props: Array<any>;

        protected /* var */_hasFriction: Boolean;

        protected /* var */_step: uint;

        protected /* var */_stepsPerTimeUnit: uint=30;
    }
}


class PhysicsProp
{
    public PhysicsProp(arg1: string, arg2: number, arg3: number, arg4: number, arg5: number, arg6: uint)
    {
        super();
        this.property = arg1;
        var loc1;
        this.value = loc1 = arg2;
        this.start = loc1;
        this.velocity = arg3 || 0;
        this.v = this.velocity / arg6;
        if (arg4 || arg4 == 0) 
        {
            this.acceleration = arg4;
            this.a = this.acceleration / (arg6 * arg6);
        }
        else 
        {
            this.a = loc1 = 0;
            this.acceleration = loc1;
        }
        this.friction = arg5 || arg5 == 0 ? 1 - arg5 : 1;
        return;
    }

    public /* var */property: string;

    public /* var */start: number;

    public /* var */velocity: number;

    public /* var */acceleration: number;

    public /* var */friction: number;

    public /* var */v: number;

    public /* var */a: number;

    public /* var */value: number;
